

import 'dart:convert';

class MarketPrice {

  late int _id;
  late num? _price;
  late DateTime? _time;

  int get id => _id;

  ///当前价
  num get lastPrice => _price ?? 0;

  DateTime get dateTime => _time ?? DateTime.now();

  MarketPrice();

  MarketPrice.fromJson(Map data) {
    _id = int.tryParse(data['cr']['id']?.toString() ?? '') ?? 0;
    _price = num.tryParse(data['cr']['p']?.toString() ?? '') ?? 0;
    _time = DateTime.fromMillisecondsSinceEpoch(
        int.tryParse(data['t']?.toString() ?? '') ?? 0);
  }

  @override
  String toString() => json.encode({
    'id': id,
    'lastPrice': lastPrice,
    'time': dateTime.millisecondsSinceEpoch,
  });

  bool equals(Object? obj) {
    if (null == obj || obj is! MarketPrice) {
      return false;
    }
    MarketPrice other = obj;
    return other.id == this.id &&
        other.lastPrice == this.lastPrice &&
        other.dateTime == this.dateTime;
  }
}